(function($, window, document, undefined){
  
  function validTel(tel){
    if(tel.length == 0){
      return "手机号码不能为空！";
    }
    if(!/^1[3-8]\d{9}$/.test(tel)){
      return "手机号码有误！";
    }
    return "";
  }
  
  function validPW(pw){
    if(pw.length == 0){
      return "密码不能为空！";
    }
    // add here
    return "";
  }
  
  $(function(){
    
    $("input", ".modifyInfo-form").on("focus", function(e){
      $(this).removeClass().addClass("editing").removeAttr("placeholder");
    })
    .on("blur", function(e){
      var $this = $(this)
      $(this).removeClass().addClass("not-editing");
      if(!$this.val()){
        $this.attr("placeholder", "点击完美资料");
      }
    });
    
    $("#modifyConfirm>.modify").on("click", function(e){
      var $mask = $(".mask");
      var $layer = $(".layer-modified");
      var $status = $layer.children(".status");
      var status;
      var isSubmit = true;
      
      function showStatus(){
        $status.text(status);
        $mask.spin(false);
        $layer.show();
      }
      
      $("input", ".modifyInfo-form").each(function(index){
        var $this = $(this);
        if(!$this.val()){
          $this.focus();
          isSubmit = false;
          return false;
        }
      });
      
      if(isSubmit){
        $.ajax({
          url: "data/modify-info-result.json",
          type: 'POST',
          data: $('.modifyInfo-form').serialize(),
          dataType: "json",
          cache: false,
          beforeSend: function(){
            $mask.show().spin("", "#fff");
          },
          success: function(data){
            status = data.status;
            showStatus();
          },
          error: function(xhr, str){
            status = str;
            showStatus();
          }
        });
      }
    });

    $("li", ".settingGrade-form").on("click", function(){
      $("li", ".settingGrade-form").removeClass("selected");
      $(this).addClass("selected");
    });

    $("#settingConfirm>.modify").on("click", function(e){
      var $mask = $(".mask");
      var $layer = $(".layer-set");
      var $status = $layer.children(".status");
      var status;
      var $selected = $("li[class*=selected]", ".settingGrade-form").find("input");
      
      function showStatus(){
        $status.text(status);
        $mask.spin(false);
        $layer.show();
      }
      
      $.ajax({
        url: "data/setting-grade-result.json",
        type: 'POST',
        data: $selected.attr("name") + "=" + $selected.attr("value"),
        dataType: "json",
        cache: false,
        beforeSend: function(){
          $mask.show().spin("", "#fff");
        },
        success: function(data){
          status = data.status;
          showStatus();
        },
        error: function(xhr, str){
          status = str;
          showStatus();
        }
      });
    });
    
    $(".binding").on("click", function(e){
      var tel = $("#tel").val();
      var pw = $("#password").val();
      var $mask = $(".mask");
      var $layer = $(".layer-binded");
      var $status = $layer.children(".status");
      var status;
      
      function showStatus(){
        $status.text(status);
        $mask.spin(false);
        $layer.show();
      }
      
      $.ajax({
        url: "data/binding-result.json",
        type: 'POST',
        data: {
          tel: tel,
          pw: pw
        },
        dataType: "json",
        cache: false,
        beforeSend: function(){
          $mask.show().spin("", "#fff");
          status = validTel(tel);
          if(status){
            showStatus();
            return false;
          }
          status = validPW(pw);
          if(status){
            showStatus();
            return false;
          }
        },
        success: function(data){
          status = data.status;
          showStatus();
        },
        error: function(xhr, str){
          status = str;
          showStatus();
        }
      });
    });
    
    $(".close").add(".ok").on("click", function(e){
      var $this = $(this);
      $this.parent().hide();
      $this.parent().prev().hide();
    });
  
  });
})(jQuery, window, document);
